package lms.dao.impl;

import lms.dao.OperationDao;
import lms.entity.Book;
import lms.entity.Reader;
import lms.utils.BookRowMapper;
import lms.utils.DBUtils;
import lms.utils.ReaderRowMapper;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 图书增删改查
 * @author: Grape_Pip
 * @time: 2021/06/22
 */
public class ReaderDaoImpl implements OperationDao<Reader> {
    /**
     * 获取JDBCTemplate对象
     **/
    private JdbcTemplate template = new JdbcTemplate(DBUtils.getDataSource());
    /**
     * 数据库连接对象
     */
    Connection conn = null;
    /**
     * 数据库操作对象
     */
    PreparedStatement pstmt = null;
    /**
     * 结果集
     */
    ResultSet rs = null;


    @Override
    public List<Reader> selectAllInfo() {
        List<Reader> readerList = new ArrayList<Reader>();
        try {
            String sql = "select * " +
                    "from l_reader r,l_readertype rt " +
                    "WHERE r.rtype=rt.rtype;";
            // 返回 bookList
            readerList = template.query(sql, new ReaderRowMapper());
            return readerList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int deleteById(int rid) {

        try {
            String sql = "delete from l_reader " +
                    "WHERE rid=?";
            int res = template.update(sql, rid);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int updateById(int rid, Reader rd) {
        try {
            String sql = "UPDATE l_reader set rname=?,rgender=?,rtel=?,rmajor=?,rtype=? " +
                    "WHERE rid=?";
            int res = template.update(sql, rd.getReaderName(),rd.getReaderGender(),rd.getReaderTel(), rd.getReaderMajor(),rd.getReaderType().getrType(),rid);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

    }

    @Override
    public int insertInfo(Reader rd) {
        try {

            String sql = "insert into l_reader (rname,rgender,rtel, rmajor,rtype,rregistime) " +
                    "VALUES (?,?,?,?,?,?)";
            int res = template.update(sql,rd.getReaderName(),rd.getReaderGender(),rd.getReaderTel(), rd.getReaderMajor(),rd.getReaderType().getrType(),rd.getReaderRegisDate());
            return res;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Reader selectById(int rid) {
        try {
            String sql = "select * from l_reader r,l_readertype rt where rid = ? and r.rtype=rt.rtype";
            // 返回 Student对象
            return template.queryForObject(sql, new ReaderRowMapper(), rid);
        } catch (Exception e) {
//            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int updateStockById(int bid, int bNum) {
        return 0;
    }
}
